package tech.dongling.boot.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.dongling.boot.anno.Component;
import tech.dongling.boot.anno.HttpProcessor;
import tech.dongling.boot.anno.ProtocolMapping;
import tech.dongling.boot.common.base.HttpBizRequest;

// 订单HTTP处理器
@Component("orderHttpProcessor")
@ProtocolMapping({"/order", "/order/create"})
public class OrderHttpProcessor implements HttpProcessor {
    private static final Logger logger = LoggerFactory.getLogger(OrderHttpProcessor.class);
    
    private final OrderService orderService;

    // 构造函数注入依赖
    public OrderHttpProcessor(OrderService orderService) {
        this.orderService = orderService;
        logger.info("OrderHttpProcessor initialized with OrderService dependency");
    }

    @Override
    public Object handle(HttpBizRequest request) {
        logger.info("Processing order request with params: {}", request.getParams());
        
        String path = request.getPath();
        
        if ("/order".equals(path)) {
            // 获取订单信息
            String orderId = request.getParams().get("orderId");
            logger.debug("Getting order info for orderId: {}", orderId);
            
            String result = orderService.getOrderInfo(orderId);
            logger.info("Order info retrieved: {}", result);
            
            return result;
            
        } else if ("/order/create".equals(path)) {
            // 创建订单
            String productName = request.getParams().get("productName");
            logger.debug("Creating order for product: {}", productName);
            
            String orderId = orderService.createOrder(productName);
            logger.info("Order created with id: {}", orderId);
            
            return orderId;
        }
        
        logger.warn("Unknown order path: {}", path);
        return "Unknown order operation";
    }
} 